package com.web.app.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	public static final String USER = "_USER";
	
	public static final String TARGET_URL = "_TARGET_URL";

	private static final long serialVersionUID = 4428902680187134168L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();

		if (session.getAttribute(USER) == null || StringUtils.isEmpty((String) session.getAttribute(USER))) {
			HttpServletRequest request = ServletActionContext.getRequest();
			session.setAttribute(TARGET_URL, request.getRequestURI());
			return Action.LOGIN;
		}

		return invocation.invoke();
	}

}
